package com.jvtd.wxpay;

import android.content.Context;
import android.support.annotation.IntDef;
import android.text.TextUtils;

import com.tencent.mm.opensdk.constants.Build;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class JvtdWxPay {
    private static volatile JvtdWxPay instance;

    public static JvtdWxPay getInstance() {
        if (instance == null)
            synchronized (JvtdWxPay.class) {
                if (instance == null)
                    instance = new JvtdWxPay();
            }
        return instance;
    }

    private JvtdWxPay() {
    }

    private IWXAPI mWXApi;
    private WxPayBean mWxPayBean;
    private WXPayResultCallBack mCallback;

    public static final int WX_NO_OR_LOW_WX = 1;   //未安装微信或微信版本过低
    public static final int WX_ERROR_PAY_PARAM = 2;  //支付参数错误
    public static final int WX_ERROR_PAY = 3;  //支付失败

    @IntDef({WX_NO_OR_LOW_WX, WX_ERROR_PAY_PARAM, WX_ERROR_PAY})
    @Retention(RetentionPolicy.SOURCE)
    public @interface WxErrorCode {
    }

    public interface WXPayResultCallBack {
        void onSuccess(); //支付成功

        void onError(@WxErrorCode int error_code);   //支付失败

        void onCancel();    //支付取消
    }

    /**
     * 必须初始化
     *
     * @author Chenlei
     * created at 2018/9/28
     **/
    public JvtdWxPay init(Context context, String wx_appid) {
        mWXApi = WXAPIFactory.createWXAPI(context, null);
        mWXApi.registerApp(wx_appid);
        return this;
    }

    public IWXAPI getWXApi() {
        return mWXApi;
    }

    //去支付
    public void doPay(WxPayBean wxPayBean, WXPayResultCallBack callback) {
        if (mWXApi == null) {
            try {
                throw new Exception("微信支付必须初始化");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return;
        }
        mWxPayBean = wxPayBean;
        mCallback = callback;

        if(!check()) {
            if(mCallback != null) {
                mCallback.onError(WX_NO_OR_LOW_WX);
            }
            return;
        }

        if(isError()) {
            if(mCallback != null) {
                mCallback.onError(WX_ERROR_PAY_PARAM);
            }
            return;
        }

        PayReq req = new PayReq();
        req.appId = mWxPayBean.getAppid();
        req.partnerId = mWxPayBean.getPartnerid();
        req.prepayId = mWxPayBean.getPrepayid();
        req.packageValue = mWxPayBean.getPackageInfo();
        req.nonceStr = mWxPayBean.getNoncestr();
        req.timeStamp = mWxPayBean.getTimestamp();
        req.sign = mWxPayBean.getSign();

        mWXApi.sendReq(req);
    }

    //支付回调响应
    public void onResp(int error_code) {
        if(mCallback == null) {
            return;
        }
        if(error_code == 0) {   //成功
            mCallback.onSuccess();
        } else if(error_code == -1) {   //错误
            mCallback.onError(WX_ERROR_PAY);
        } else if(error_code == -2) {   //取消
            mCallback.onCancel();
        }
        mCallback = null;
    }

    //检测参数是否错误
    private boolean isError(){
        return mWxPayBean == null || TextUtils.isEmpty(mWxPayBean.getAppid()) || TextUtils.isEmpty(mWxPayBean.getPartnerid())
                || TextUtils.isEmpty(mWxPayBean.getPrepayid()) || TextUtils.isEmpty(mWxPayBean.getPackageInfo()) ||
                TextUtils.isEmpty(mWxPayBean.getNoncestr()) || TextUtils.isEmpty(mWxPayBean.getTimestamp()) ||
                TextUtils.isEmpty(mWxPayBean.getSign());
    }

    //检测是否支持微信支付
    private boolean check() {
        return mWXApi.isWXAppInstalled() && mWXApi.getWXAppSupportAPI() >= Build.PAY_SUPPORTED_SDK_INT;
    }
}
